<?php

function spValidateEmail( $email )
{
	if (!preg_match('/^[_A-z0-9-]+((\.|\+)[_A-z0-9-]+)*@[A-z0-9-]+(\.[A-z0-9-]+)*(\.[A-z]{2,4})$/',$email) )
	{
		return false;
	}

	// This will split the email into its front
	// and back (the domain) portions
	list($name, $domain) = explode('@',$email);

	if (!checkdnsrr($domain,'MX')){

		// No MX record found
		return false;
	}

	return true;
}

function spHumanToHtml( $text )
{
	return nl2br( $text );
}

function spDump( $title, $val )
{
	echo "<div style='border:solid 1px black;'>";
	echo "<h1>".$title."</h1><pre>";
	print_r( $val );
	echo "</pre></div>";
}

function spStackTrace()
{
	echo "<pre>";
	debug_print_backtrace();
	echo "</pre>";
}

function unMagicQuotify($ar) {
	$fixed = array();
	foreach ($ar as $key=>$val) {
		if (is_array($val)) {
			$fixed[stripslashes($key)] = unMagicQuotify($val);
		} else {
			$fixed[stripslashes($key)] = stripslashes($val);
		}
	}
	
	return $fixed;
}

// dumps all hooks in wordpress
function list_hooked_functions($tag=false){
 global $wp_filter;
 if ($tag) {
  $hook[$tag]=$wp_filter[$tag];
  if (!is_array($hook[$tag])) {
  trigger_error("Nothing found for '$tag' hook", E_USER_WARNING);
  return;
  }
 }
 else {
  $hook=$wp_filter;
  ksort($hook);
 }
 echo '<pre>';
 foreach($hook as $tag => $priority){
  echo "<br />&gt;&gt;&gt;&gt;&gt;\t<strong>$tag</strong><br />";
  ksort($priority);
  foreach($priority as $priority => $function){
  echo $priority;
  foreach($function as $name => $properties) echo "\t$name<br />";
  }
 }
 echo '</pre>';
 return;
}

function getV( $ar, $indexName, $defaultValue="" )
{
	if ( isset($ar[$indexName]) )
		return $ar[$indexName];
		
	return $defaultValue;
}

function spCreatePage( $page_title )
{
	global $wpdb;

    $the_page_title = $page_title;
	
	$the_page = get_page_by_title( $the_page_title );

    if ( ! $the_page ) {

        // Create post object
        $_p = array();
        $_p['post_title'] 		= $the_page_title;
        $_p['post_content'] 	= "This text may be overridden by the plugin. You shouldn't edit it.";
        $_p['post_status'] 		= 'publish';
        $_p['post_type'] 		= 'page';
        $_p['comment_status'] 	= 'closed';
        $_p['ping_status'] 		= 'closed';
        $_p['post_category'] 	= array(1); // the default 'Uncatrgorised'

        // Insert the post into the database
        $the_page_id = wp_insert_post( $_p );
    }
    else 
	{
        // the plugin may have been previously active and the page may just be trashed...
        $the_page_id = $the_page->ID;

        //make sure the page is not trashed...
        $the_page->post_status = 'publish';
        $the_page_id = wp_update_post( $the_page );
    }	
}

function spRemovePage( $page_title )
{
	global $wpdb;

    $the_page = get_page_by_title( $the_page_title );

    //  the id of our page...
    $the_page_id = $the_page->ID;
    if( $the_page_id ) {

        wp_delete_post( $the_page_id ); // this will trash, not delete
    }	
}

function spImplode($glue,$pieces){
    if (is_array($pieces)){
        $str = '';
        foreach($pieces as $key => $val){
            if (strlen($val) > 0){
                $str .= $val.$glue;
            }
        }
        return trim($str,$glue);
    } else {
    return $pieces;
    }
}

function spExplode( $glue, $str, $trim=false )
{
	$out = array();
    $arr = explode($glue, $str);
    foreach ( $arr as $item )
	{
        if ( $item!="" )
		{
			if ( $trim )
				$item = trim( $item );
				
           $out[] = $item;
		}
	}
    return $out;
}

function spArraySortByArrayValue( &$ar, $sortby )
{
	$arNew = array();
	foreach ( $ar as $p )
	{
		$key = $p[$sortby];
		$arNew[$key] = $p;
	}
	ksort( $arNew );
	$ar = $arNew;
}

class spUtil
{
	static public function inCSV( $needle, $csv )
	{
		$ar = spExplode( ",", $csv );
		if ( in_array($needle, $ar ) )
			return true;
			
		return false;
	}
}

?>